/**
 * [1 Timothy 3:16-17 ESV]
 * All Scripture is breathed out by God and profitable
 * for teaching, for reproof, for correction, and for training in righteousness,
 * that the man of God may be complete, equipped for every good work.
 *
 * BibleReader is an simple application for you to study God's Word.
 * The main user of this application maybe the pastor who's main language
 * is Chinese.
 *
 * You can use it totally free, and if you want to get source code of
 * this application, please email me at dweng123@gmail to get the source
 * code.
 */
#include <QSplitter>
#include <QGridLayout>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QCompleter>
#include "bibledictionarywidget.h"

BibleDictionaryWidget::BibleDictionaryWidget(BibleReaderCore *brc, QString dn, QWidget *parent) :
    QWidget(parent)
{
    brCore = brc;
    dictName = dn;

    if (brCore->getCurrentDict() == dictName) {
        isCurrent = true;
    }

    if (wordsList.empty()) {
        wordsList = brCore->getAllWordsAndExplainations(dictName);
    }

    LOG_INFO() << "create dict widget";
    createWidgets();
    LOG_INFO() << "created dict widget";
}

BibleDictionaryWidget::~BibleDictionaryWidget()
{
    destoryWidgets();
}

void BibleDictionaryWidget::createWidgets()
{
    dictShowExplaination = new BibleDictBrowser(brCore, this);

    dictWordSearch = new QLineEdit(this);
    connect(dictWordSearch, SIGNAL(returnPressed()), this, SLOT(showExplaination()));
    dictWordSearch->setPlaceholderText(brCore->getDictInfoByName(dictName).getPlaceHolderText());
    QCompleter *temp = new QCompleter(wordsList.keys());
    temp->setCompletionMode(QCompleter::PopupCompletion);
    temp->setFilterMode(Qt::MatchContains);
    dictWordSearch->setCompleter(temp);

    QVBoxLayout *vLayout = new QVBoxLayout(this);
    vLayout->setContentsMargins(0, 0, 0, 0);
    vLayout->addWidget(dictWordSearch);
    vLayout->addWidget(dictShowExplaination);
}

void BibleDictionaryWidget::destoryWidgets()
{
    delete dictShowExplaination;
    delete dictWordSearch;
}

QString BibleDictionaryWidget::compileExplaination(QString exp)
{
    QRegularExpression rx("\\d+:\\d+");
    QRegularExpressionMatch match = rx.match(exp);
    if (match.hasMatch()) {
        for (int i=0; i <= match.lastCapturedIndex(); i++) {
            QString refs = match.captured(i);
        }
    }

    return exp
            .replace(QString("\\r\\n"), QString("<br />"))
            .prepend("<style>.exp{color: green;} p {font-family:%1;}</style><p>")
            .append("</p>").arg(brCore->getConfigurator()->getBibleTextFontFamily());
}

QString BibleDictionaryWidget::getDictName() const
{
    return dictName;
}

void BibleDictionaryWidget::setDictName(const QString &value)
{
    dictName = value;
}



void BibleDictionaryWidget::showExplaination(QListWidgetItem* current,
                                             QListWidgetItem* previous)
{
    QString word;
    if (current) {
        word = current->data(0).toString();
    } else {
        word = previous->data(0).toString();
    }
    QString explaination = wordsList.value(word);
    dictShowExplaination->setText(compileExplaination(explaination));
}


void BibleDictionaryWidget::showExplaination()
{
    QLineEdit *temp = qobject_cast<QLineEdit*>(sender());
    QString itemName = temp->text();
    QString explaination = wordsList.value(itemName, tr("Can not find any explaination for this word"));
    dictShowExplaination->setText(compileExplaination(explaination));
}

void BibleDictionaryWidget::showExplaination(QString itemName)
{
    QString explaination = wordsList.value(itemName, tr("Can not find any explaination for this word"));
    dictShowExplaination->setText(compileExplaination(explaination));
}

